﻿using System.Security.Claims;
using System.Security.Principal;
using ZeroSum.Constants;

namespace ZeroSum.Extensions;

/// <summary>
///     <see cref="ClaimsIdentity" />扩展操作类
/// </summary>
public static class ClaimsIdentityExtensions
{
    /// <summary>
    ///     获取用户ID
    /// </summary>
    public static long? GetUserId(this IIdentity? identity)
    {
        if (identity is not ClaimsIdentity claimsIdentity) return null;
        var value = claimsIdentity.FindFirst(ClaimConst.CLAINM_USERID)?.Value;
        return long.Parse(value!);
    }

    /// <summary>
    ///     获取用户名
    /// </summary>
    public static string? GetUserName(this IIdentity identity)
    {
        if (!(identity is ClaimsIdentity claimsIdentity)) return null;
        return claimsIdentity.FindFirst(ClaimConst.CLAINM_NAME)?.Value;
    }
}